//
//  LPNewsSearchViewController.m
//  LovingPartyBuilding
//
//  Created by 刘应平 on 17/5/10.
//  Copyright © 2017年 刘应平. All rights reserved.
//

#import "LPNewsSearchViewController.h"
#import "MyBranchDynamicjTableViewCell.h"
#import "MyBranchDynamicShowBigTableViewCell.h"
#import "STPhotoBroswer.h"
#import "LPDCDetailModel.h"
#import "LPWKHasCommentAndHeadLineViewController.h"
#define MOREIMAGE @"moreimage"
#define ONEIMAGE @"oneimage"
@interface LPNewsSearchViewController ()<UITableViewDelegate,UITableViewDataSource>
/**  table  */
@property (strong, nonatomic) UITableView  *tableView;
/**  数据  */
@property (strong, nonatomic) NSMutableArray  *dataSouce;
/**  page  */
@property (assign, nonatomic) NSInteger  page;
/**  搜索的关键字  */
@property (strong, nonatomic) NSString  *keyWord;
/**  上一个搜索的字  */
@property (strong, nonatomic) NSString  *lastString;

/** 滑动 */
@property (nonatomic, strong) ZJRefurbish *refurbish;


@end

@implementation LPNewsSearchViewController


- (void)viewDidLoad {
    [super viewDidLoad];
    self.keyWord = @"";
    /**  刚开始默认显示一页  */
    self.page = 1;
    /**  添加通知  */
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(searchText:) name:@"post" object:nil];
    __weak typeof(self) weakSelf = self;
    [self.refurbish headerToScollview:self.tableView Block:^{
        weakSelf.page = 1;
        [weakSelf netWorking_header];
        
    }];
    self.tableView.mj_header.hidden = YES;
    [self.refurbish footerToScollview:self.tableView Block:^{
        weakSelf.page ++;
        [weakSelf netWorking_footer];
    }];
    self.tableView.mj_footer.hidden = YES;
    [self layoutUI];
}
#pragma mark - 数据
//下拉刷新
- (void)netWorking_header{
    __weak typeof(self) weakSelf = self;
    NSString *page = [NSString stringWithFormat: @"%ld",(long)self.page];
    NSDictionary *paramer = @{
                              @"PAGESTART": page,
                              @"PAGECOUNT": @"5",
                              @"USERID": ZJUSERINFORMODEL.CURRENT_USER_MODEL.ID,
                              @"UNITID": ZJUSERINFORMODEL.CURRENT_DEPT_MODEL.ID,
                              @"KEYWORD": self.keyWord,
                              @"ACCOUNTID": ZJUSERINFORMODEL.ORG_ACCOUNT.ID};

    
    [NetWorkingManager postWithUrlString:[NetWorkingURL getFindNewsForSolr] Parameter:paramer Success:^(id object) {
        NetWorkModel *model = [NetWorkModel yy_modelWithJSON:object];
        if ([model.STATE isEqualToString:@"20000"]) {
            NSArray *array = object[@"NEWS"];
            weakSelf.dataSouce = [[NSArray yy_modelArrayWithClass:[LPDCDetailModel class] json:array] mutableCopy];
            [weakSelf.tableView reloadData];
            weakSelf.tableView.mj_footer.hidden = NO;
        }else if ([model.STATE isEqualToString:@"50003"]){
            
            [weakSelf.dataSouce removeAllObjects];
            [weakSelf.tableView reloadData];
            if (weakSelf.dataSouce.count == 0) {
                [[LPShowNoDataManager showNoDataManager] showEmptyViewWithView:weakSelf.tableView Type:1];
            }
        }
        [weakSelf.tableView.mj_header endRefreshing];
    } Fail:^(id object) {
        
        [weakSelf.tableView.mj_header endRefreshing];
        if (weakSelf.dataSouce.count == 0) {
            [[LPShowNoDataManager showNoDataManager] showEmptyViewWithView:weakSelf.tableView Type:0];
            [LPShowNoDataManager showNoDataManager].noContentViewTapedBlock = ^{
                [weakSelf.tableView.mj_header beginRefreshing];
            };
        }else{
            [YJProgressHUD showMsgWithoutView:@"请求失败"];
        }
    }];
    
}
//下拉刷新
- (void)getData{
    __weak typeof(self) weakSelf = self;
    NSString *page = [NSString stringWithFormat: @"%ld",(long)self.page];
    NSDictionary *paramer = @{
                              @"PAGESTART": page,
                              @"PAGECOUNT": @"5",
                              @"USERID": ZJUSERINFORMODEL.CURRENT_USER_MODEL.ID,
                              @"UNITID": ZJUSERINFORMODEL.CURRENT_DEPT_MODEL.ID,
                              @"KEYWORD": self.keyWord,
                              @"ACCOUNTID": ZJUSERINFORMODEL.ORG_ACCOUNT.ID};
    
    [NetWorkingManager postWithUrlString:[NetWorkingURL getFindNewsForSolr] Parameter:paramer Success:^(id object) {
        
        NetWorkModel *model = [NetWorkModel yy_modelWithJSON:object];
        if ([model.STATE isEqualToString:@"20000"]) {
            NSArray *array = object[@"NEWS"];
            weakSelf.dataSouce = [[NSArray yy_modelArrayWithClass:[LPDCDetailModel class] json:array] mutableCopy];
            self.tableView.mj_footer.hidden = NO;
            [weakSelf.tableView reloadData];
        }else if ([model.STATE isEqualToString:@"50003"]){
            
            [YJProgressHUD showSuccess:object[@"ERROR_MSG"] inview:self.view];
            [self.dataSouce removeAllObjects];
            [weakSelf.tableView reloadData];
        }

    } Fail:^(id object) {
       
    }];
    
}



//上拉加载
- (void)netWorking_footer{
    __weak typeof(self) weakSelf = self;
    NSString *page = [NSString stringWithFormat: @"%ld",(long)self.page];
    NSDictionary *paramer = @{
                              @"PAGESTART": page,
                              @"PAGECOUNT": @"5",
                              @"USERID": ZJUSERINFORMODEL.CURRENT_USER_MODEL.ID,
                              @"UNITID": ZJUSERINFORMODEL.CURRENT_DEPT_MODEL.ID,
                              @"KEYWORD": self.keyWord,
                              @"ACCOUNTID": ZJUSERINFORMODEL.ORG_ACCOUNT.ID};
    
    [NetWorkingManager postWithUrlString:[NetWorkingURL getFindNewsForSolr] Parameter:paramer Success:^(id object) {
        NetWorkModel *model = [NetWorkModel yy_modelWithJSON:object];
        if ([model.STATE isEqualToString:@"20000"]) {
            NSArray *array = object[@"NEWS"];
            if (array.count == 0) {
                [YJProgressHUD showMessage:@"没有数据" inView:[UIApplication sharedApplication].keyWindow];
                [weakSelf.tableView.mj_footer endRefreshing];
                return ;
                
            }
            [weakSelf.dataSouce addObjectsFromArray:[NSArray yy_modelArrayWithClass:[LPDCDetailModel class] json:array]];
            [weakSelf.tableView reloadData];
        }
        [weakSelf.tableView.mj_footer endRefreshing];
    } Fail:^(id object) {
        [weakSelf.tableView.mj_footer endRefreshing];
    }];
    
}
#pragma mark - 数据

- (void)searchText:(NSNotification *)nioti{
    self.keyWord = nioti.userInfo[@"text"];
    if (self.keyWord.length == 0) {
        self.tableView.mj_header.hidden = YES;
        self.tableView.mj_footer.hidden = YES;
        [self.dataSouce removeAllObjects];
        [self.tableView reloadData];
       
    }else{
        self.tableView.mj_header.hidden  = NO;
        [self.tableView.mj_header beginRefreshing];
        
        
    }
}

- (void)dealloc{
    [[NSNotificationCenter defaultCenter] removeObserver:self];
}




#pragma mark - 界面
- (void)layoutUI{
    [self.view addSubview:self.tableView];
    
}


/**  UITableViewDataSource,UITableViewDelegate  */
/**  返回组个数  */
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return 1;
}
/**  返回cell个数  */
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return self.dataSouce.count;
}
/**  返回cell高度  */
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    LPDCDetailModel *model = self.dataSouce[indexPath.row];
    if ([model.DISPLAY isEqualToString:@"2"]) {
        return 200 * [UIScreen zj_screenHScale];
    }else{
        return 155 * [UIScreen zj_screenHScale];
    }
    
}
/**  cell初始化  */
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    LPDCDetailModel *model = self.dataSouce[indexPath.row];
    if ([model.DISPLAY isEqualToString:@"2"]) {
        MyBranchDynamicShowBigTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:MOREIMAGE forIndexPath:indexPath];
        cell.model = model;
        return cell;
        
    }else{
        MyBranchDynamicjTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:ONEIMAGE forIndexPath:indexPath];
        cell.model = model;
        return cell;
        
    }
    
    
    
}
/**  cell的点击事件  */
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    LPDCDetailModel *model = self.dataSouce[indexPath.row];
    LPWKHasCommentAndHeadLineViewController *vc = [LPWKHasCommentAndHeadLineViewController new];
    vc.model = model;
    [self.navigationController pushViewController:vc animated:YES];
    
}
#pragma mark - 懒加载
/**  初始化tableView  */
- (UITableView *)tableView{
    if (!_tableView) {
        _tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, self.view.bounds.size.width, self.view.bounds.size.height - LP_TopHeight - 50 * [UIScreen zj_screenHScale]) style:UITableViewStylePlain];
        _tableView.dataSource = self;
        _tableView.delegate = self;
        _tableView.separatorColor = [UIColor clearColor];
        _tableView.backgroundColor = [UIColor themeGrayColor];
        _tableView.tableFooterView = [UIView new];
        [_tableView registerClass:[MyBranchDynamicjTableViewCell class] forCellReuseIdentifier:ONEIMAGE];
        [_tableView registerClass:[MyBranchDynamicShowBigTableViewCell class] forCellReuseIdentifier:MOREIMAGE];

    }
    return _tableView;
}

- (NSMutableArray *)dataSouce{
    if (!_dataSouce) {
        _dataSouce = [NSMutableArray array];
    }
    return _dataSouce;
}
- (ZJRefurbish *)refurbish{
    if (!_refurbish) {
        _refurbish = [ZJRefurbish new];
    }
    return _refurbish;
}
@end
